package com.smartens.boit.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.smartens.boit.R;
import com.smartens.boit.bean.StatusDetailBean;

import java.util.List;

/**
 * @Description 能耗控制适配器
 * @Author songtt
 * @Date 2021/3/4
 */

public class EnergyAdapter extends RecyclerView.Adapter<EnergyAdapter.ViewHolder> {
    private List<StatusDetailBean> mList;
    private OnItemClickListener onItemClickListener;

    public EnergyAdapter(List<StatusDetailBean> list) {
        mList = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //将我们自定义的item布局R.layout.item_one转换为View
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_energy, parent, false);
        //将view传递给我们自定义的ViewHolder
        ViewHolder holder = new ViewHolder(view);
        //返回这个MyHolder实体
        return holder;
    }

    /**
     * 通过方法提供的ViewHolder，将数据绑定到ViewHolder中
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.DevicesName.setText(mList.get(position).getDevicesName());
        holder.DevicesNum.setText("设备编号 " + mList.get(position).getDevicesNum());
        holder.TotalPower.setText(mList.get(position).getTotalPower());

        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, holder.getAdapterPosition() + 1);
                }
            }
        });
    }

    /**
     * 获取数据源总的条数
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout linearLayout;
        TextView DevicesNum;
        TextView DevicesName;
        TextView TotalPower;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            linearLayout = itemView.findViewById(R.id.lin);
            TotalPower = itemView.findViewById(R.id.TotalPower);
            DevicesNum = itemView.findViewById(R.id.DevicesNum);
            DevicesName = itemView.findViewById(R.id.DevicesName);
        }
    }

    /**
     * 供外部调用设置监听
     *
     * @param onItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    /**
     * 自定义的接口
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void getData(List<StatusDetailBean> list) {

        this.mList.clear();
        this.mList.addAll(list);
    }
}
